﻿using System;
using BleQua.Extensions;

namespace BleQua.Data {
	public class ManagerCenter : IDisposable {
		private BleQua.Shared.DataEntities.ProfileContext DataContext { get; set; }
		public bool IsLoaded { get; set; }
		public string Username { get; set; }
		public string Password { get; set; }

		public string BuildConnectionString(string username, string password) {
			if (string.IsNullOrEmpty(password))
				return "metadata=res://*/DataEntities.ProfileModel.csdl|res://*/DataEntities.ProfileModel.ssdl|res://*/DataEntities.ProfileModel.msl;provider=System.Data.SqlServerCe.4.0;provider connection string=\"data source=|DataDirectory|\\Profiles\\" + username + ".sdf\"";
			else
				return "metadata=res://*/DataEntities.ProfileModel.csdl|res://*/DataEntities.ProfileModel.ssdl|res://*/DataEntities.ProfileModel.msl;provider=System.Data.SqlServerCe.4.0;provider connection string=\"data source=|DataDirectory|\\Profiles\\" + username + ".sdf;password='" + password + "'\"";
		}

		public void LoadDatabase( ) {
			this.IsLoaded = false;
			if (this.DataContext != null) {
				this.DataContext.Dispose( );
				this.DataContext = null;
			}
			this.DataContext = new Shared.DataEntities.ProfileContext(this.BuildConnectionString(this.Username, this.Password));
			if (DataContext != null) {
				this.Password = null;
				this.ApplicationState = new ApplicationStateManager( );
				this.Profile = new ProfileManager(DataContext);
				this.Settings = new SettingsManager(DataContext);
				this.Extensions = new ExtensionsManager("Plugins");
				this.Providers = new ProvidersManager(this.Extensions);
				this.Templates = new TemplatesManager(this.Extensions);
				this.Accounts = new AccountsManager(DataContext, this.Providers);
				this.Contacts = new ContactsManager(DataContext, this.Providers);
				this.IsLoaded = true;
			}
		}

		public AccountsManager Accounts { get; private set; }
		public ApplicationStateManager ApplicationState { get; private set; }
		public ContactsManager Contacts { get; private set; }
		public ExtensionsManager Extensions { get; private set; }
		public ProfileManager Profile { get; private set; }
		public ProvidersManager Providers { get; private set; }
		public SettingsManager Settings { get; private set; }
		public TemplatesManager Templates { get; private set; }

		public void UnloadData( ) {
			if (this.IsLoaded)
				this.IsLoaded = false;
			if (this.Username != null) {
				this.Username = null;
			}
			if (this.Password != null) {
				this.Password = null;
			}
			if (this.Contacts != null) {
				this.Contacts.Dispose( );
				this.Contacts = null;
			}
			if (this.Accounts != null) {
				this.Accounts.Dispose( );
				this.Accounts = null;
			}
			if (this.Templates != null) {
				this.Templates.Dispose( );
				this.Templates = null;
			}
			if (this.Providers != null) {
				this.Providers.Dispose( );
				this.Providers = null;
			}
			if (this.Extensions != null) {
				this.Extensions.Dispose( );
				this.Extensions = null;
			}
			if (this.Settings != null) {
				this.Settings.Dispose( );
				this.Settings = null;
			}
			if (this.Profile != null) {
				this.Profile.Dispose( );
				this.Profile = null;
			}
			if (this.ApplicationState != null) {
				this.ApplicationState.Dispose( );
				this.ApplicationState = null;
			}
			if (this.DataContext != null) {
				this.DataContext.Dispose( );
				this.DataContext = null;
			}
		}

		public void Dispose( ) {
			this.UnloadData( );
		}
	}
}
